package com.sky.sky_server.controller.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sky.sky_common.result.Result;
import com.sky.sky_pojo.dto.SetmealDTO;
import com.sky.sky_pojo.entity.SetmealQueryPage;
import com.sky.sky_server.service.impl.SetmealServiceImpl;

import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;

@RestController("adminSetmealController")
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealServiceImpl setmealServiceImpl;

    /**
     * 新增套餐
     */
    @PostMapping
    @CacheEvict(value = "setmealCache", key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO) {
        log.info("新增套餐，套餐信息：{}", setmealDTO);
        return Result.success(setmealServiceImpl.save(setmealDTO));
    }

    /**
     * 分页查询套餐
     * @param setmealQueryPage
     * @return
     */
    @GetMapping("/page")
    public Result page(@Valid SetmealQueryPage setmealQueryPage) {
        log.info("分页查询套餐，查询条件：{}", setmealQueryPage);
        return Result.success(setmealServiceImpl.page(setmealQueryPage));
    }

    /**
     * 根据套餐id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id) {
        log.info("根据套餐id查询套餐，套餐id：{}", id);
        return Result.success(setmealServiceImpl.selectById(id));  
    }

    /**
     * 修改套餐
     */
    @PutMapping
    @CacheEvict(value = "setmealCache", key = "#setmealDTO.categoryId")
    public Result update(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐，套餐信息：{}", setmealDTO);
        int res = setmealServiceImpl.update(setmealDTO);
        if(res > 0) {
            return Result.success("修改套餐成功 "+res);
        }
        return Result.error("修改套餐失败");
    }

    /**
     * 修改套餐状态
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmealCache", allEntries = true)
    public Result updateStatus(@PathVariable Integer status, @RequestParam Long id) {
        log.info("修改套餐状态，状态：{}，套餐id列表：{}", status, id);
        int res = setmealServiceImpl.updateStatus(status, id);
        if(res > 0) {
            return Result.success("修改套餐状态成功 "+res);
        }
        return Result.error("修改套餐状态失败");
    }

    /**
     * 删除套餐 批量
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public Result delete(@RequestParam List<Long> ids) {
        return Result.success(setmealServiceImpl.deleteSetmealByIds(ids));
    }

}
